package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.Scheme;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.SchemeDao;
import cn.edu.nju.software.cdiy.dao.ValueDao;
import cn.edu.nju.software.cdiy.util.BusinessConstant;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

public class SchemeDaoImpl implements SchemeDao {

	@Autowired
	private SessionFactory sessionFactory;

	@Autowired
	private ValueDao valueDaoImpl;

	public ValueDao getValueDaoImpl() {
		return valueDaoImpl;
	}

	public void setValueDaoImpl(ValueDao valueDaoImpl) {
		this.valueDaoImpl = valueDaoImpl;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Scheme getSchemeById(Long id) {
		Session session = sessionFactory.getCurrentSession();

		Scheme s = null;
		try {

			s = (Scheme) session.load(Scheme.class, id);

			s.getCpu();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return s;

	}

	@Override
	public List<Scheme> loadAllScheme() {
		Session session = sessionFactory.getCurrentSession();

		List list = null;
		try {

			list = session.createQuery("from Scheme").list();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return list;
	}

	@Override
	public List<Scheme> loadUserScheme(Long userid) {
		Session session = sessionFactory.getCurrentSession();

		List list = null;
		try {
			Query query = session
					.createQuery("from Scheme s where s.userId=:userid order by s.time desc");
			query.setLong("userid", userid);
			list = query.list();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Scheme save(Scheme scheme) {
		Session session = sessionFactory.getCurrentSession();

		try {

			session.save(scheme);

		} catch (Exception e) {

			e.printStackTrace();
		}
		return scheme;

	}

	@Override
	public List<Scheme> searchBySchemeName(String name) {
		Session session = sessionFactory.getCurrentSession();

		List<Scheme> schemeList = null;
		try {

			String sql = "from Scheme s where s.schemeName like '%'||:name||'%' ";
			Query query = session.createQuery(sql);
			query.setParameter("name", name);
			schemeList = query.list();

		} catch (Exception e) {

			e.printStackTrace();
		}
		return schemeList;
	}

	@Override
	public List<Scheme> getIntelBrand() {
		Session session = sessionFactory.getCurrentSession();

		List<Scheme> returnInt = null;
		try {

			Query query = session
					.createQuery("from Scheme  s  where s.cpu.brand.brand=:intel");
			query.setString("intel", "Intel");

			returnInt = query.list();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return returnInt;
	}

	@Override
	public List<Scheme> getOrderByHot() {
		Session session = sessionFactory.getCurrentSession();

		List<Scheme> returnInt = null;
		try {

			Query query = session
					.createSQLQuery(
							"select s.* from scheme s ,CPU c, BRD_CPU b where s.cpu=c.id and c.brand=b.id and b.id=2  order by "
									+ "(TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ")
					.addEntity(Scheme.class);
			Query query2 = session
					.createQuery("select s from Scheme s,CPU c where s.cpu=c and c.brand.brand='Intel' order by "
							+ "(TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");

			Query query3 = session
					.createQuery("select s from Scheme s where s.graphics.coreManu.brand='NVIDIA' order by "
							+ "(TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");

			returnInt = query3.list();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return returnInt;
	}

	@Override
	public Map<String, List<Item>> getFilterBoard() {
		Map<String, List<Item>> returnMap = new HashMap<String, List<Item>>();
		List<Item> brandItem = new ArrayList<Item>();
		List<Value> brandList = valueDaoImpl.getBrandList();
		// System.out.println(brandList.size());
		Iterator<Value> vIter = brandList.iterator();
		while (vIter.hasNext()) {
			Value v = vIter.next();
			Item i = new Item(v.getValue(), v.getKey());
			brandItem.add(i);
		}

		returnMap.put(BusinessConstant.SCHEME_BRAND_STRING, brandItem);
		List<Item> priceItem = new ArrayList<Item>();
		List<Value> priceList = valueDaoImpl.getPriceList();

		vIter = priceList.iterator();
		while (vIter.hasNext()) {
			Value v = vIter.next();
			Item i = new Item(v.getValue(), v.getKey());
			priceItem.add(i);
		}

		returnMap.put(BusinessConstant.SCHEME_PRICE_STRING, priceItem);
		List<Item> typeItem = new ArrayList<Item>();
		List<Value> typeList = valueDaoImpl.getTypeList();

		vIter = typeList.iterator();
		while (vIter.hasNext()) {
			Value v = vIter.next();
			Item i = new Item(v.getValue(), v.getKey());
			typeItem.add(i);
		}

		returnMap.put(BusinessConstant.SCHEME_TYPE_STRING, typeItem);
		return returnMap;
	}

	@Override
	public List<Scheme> filter(List<Integer> brandList,
			List<Integer> priceList, List<Integer> typeList, SortType st,
			SortWay sw, int page) {
		StringBuffer brandCheckString = new StringBuffer();
		StringBuffer priceCheckString = new StringBuffer();
		StringBuffer typeCheckString = new StringBuffer();
		Session s = sessionFactory.getCurrentSession();
		if (brandList == null) {
			brandCheckString = new StringBuffer();

		} else {
			Iterator<Integer> iter = brandList.iterator();
			int i = 0;
			while (iter.hasNext()) {

				if (i == 0) {
					i = 1;
				} else {
					brandCheckString.append(" or ");
				}

				Query q = (Query) s
						.createQuery("from Value v where v.key=:key");
				q.setInteger("key", iter.next());
				Value v = (Value) q.uniqueResult();
				String str1 = v.getValue();

				if (str1.equals(BusinessConstant.BARND_CONSTANT[0])) {
					brandCheckString.append(" ( s.cpu.brand.brand='Intel') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[1])) {
					brandCheckString.append(" ( s.cpu.brand.brand='AMD') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[2])) {
					brandCheckString
							.append(" (s.graphics.coreManu.brand='NVIDIA') ");
				}

				else if (str1.equals(BusinessConstant.BARND_CONSTANT[3])) {
					brandCheckString
							.append(" (s.graphics.coreManu.brand='AMD-ATI') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[4])) {
					brandCheckString
							.append(" (s.graphics.coreManu.brand='AMD') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[5])) {
					brandCheckString.append(" (s.graphics is null) ");
				}
			}
		}
		if (priceList == null) {
			priceCheckString = new StringBuffer();

		} else {
			Iterator<Integer> iter = priceList.iterator();
			int i = 0;
			while (iter.hasNext()) {
				if (i == 0) {
					i = 1;
				} else {
					priceCheckString.append(" or ");
				}
				Query q = (Query) s
						.createQuery("from Value v where v.key=:key");
				q.setInteger("key", iter.next());
				Value v = (Value) q.uniqueResult();
				String str1 = v.getValue();

				String str2 = str1.substring(0, str1.indexOf('元'));

				if (str2.indexOf('-') != -1) {
					priceCheckString.append(" (s.price>"
							+ str2.substring(0, str2.indexOf('-'))
							+ " and s.price<="
							+ str2.substring(str2.indexOf('-') + 1) + ") ");
				}
				if (str1.indexOf('上') != -1) {
					priceCheckString.append(" (s.price>" + str2 + ") ");
				}
				if (str1.indexOf('下') != -1) {
					priceCheckString.append(" (s.price<=" + str2 + ") ");
				}

			}
		}

		if (typeList == null) {
			typeCheckString = new StringBuffer();
		} else {
			Iterator<Integer> iter = typeList.iterator();
			int i = 0;
			while (iter.hasNext()) {
				if (i == 0) {
					i = 1;
				} else {
					typeCheckString.append(" or ");
				}
				Query q = (Query) s
						.createQuery("from Value v where v.key=:key");
				q.setInteger("key", iter.next());
				Value v = (Value) q.uniqueResult();
				String str1 = v.getValue();
				if (str1.equals(BusinessConstant.TYPE_CONSTANT[0])) {
					typeCheckString.append(" (s.type.id=2081) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[1])) {
					typeCheckString.append(" (s.type.id=2082) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[2])) {
					typeCheckString.append(" (s.type.id=2083) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[3])) {
					typeCheckString.append(" (s.type.id=2084) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[4])) {
					typeCheckString.append(" (s.type.id=2085) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[5])) {
					typeCheckString.append(" (s.type.id=2086) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[6])) {
					typeCheckString.append(" (s.type.id=2087) ");
				}
			}
		}
		/*
		 * PRICE, PERFORMANCE, W_SCORE, Z_SCORE, NEW, HOT, ASC, DESC, DEFAULT
		 */
		String orderString = new String();
		switch (st) {
		case PRICE: {
			if (sw == SortWay.ASC) {
				orderString = " order by s.price asc ";

			} else if (sw == SortWay.DESC) {
				orderString = " order by s.price desc ";
			} else {
				orderString = " order by s.price asc ";
			}
			break;
		}
		case PERFORMANCE: {
			if (sw == SortWay.ASC) {
				orderString = " order by s.performance asc ";
			} else if (sw == SortWay.DESC) {
				orderString = " order by s.performance asc ";
			} else {
				orderString = " order by s.performance asc ";
			}
			break;
		}
		case W_SCORE: {
			if (sw == SortWay.ASC) {
				orderString = " order by s.wscore asc ";
			} else if (sw == SortWay.DESC) {
				orderString = " order by s.wscore desc ";
			} else {
				orderString = " order by s.wscore asc ";
			}
			break;
		}
		case Z_SCORE: {
			if (sw == SortWay.ASC) {
				orderString = " order by s.zscore asc ";
			} else if (sw == SortWay.DESC) {
				orderString = " order by s.zscore desc ";
			} else {
				orderString = " order by s.zscore asc ";
			}
			break;
		}
		case NEW: {
			if (sw == SortWay.ASC) {
				orderString = " order by s.time asc ";
			} else if (sw == SortWay.DESC) {
				orderString = " order by s.time asc ";
			} else {
				orderString = " order by s.time asc ";
			}
			break;
		}
		case HOT: {
			if (sw == SortWay.ASC) {
				orderString = " order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) asc ";
			} else if (sw == SortWay.DESC) {
				orderString = " order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) desc ";
			} else {
				orderString = " order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) asc ";
			}
			break;
		}
		}
		StringBuffer sqlString = new StringBuffer();
		if (brandCheckString.length() == 0 && priceCheckString.length() == 0
				&& typeCheckString.length() == 0)

		{
			sqlString.append("from Scheme s ");
		} else {
			sqlString.append("from Scheme s where ");
			int i1 = 0;
			int i2 = 0;
			int i3 = 0;
			if (brandCheckString.length() != 0) {
				i1 = 1;
				sqlString.append(" ( ");
				sqlString.append(brandCheckString);
				sqlString.append(" ) ");
			}
			if (priceCheckString.length() != 0) {
				if (i1 == 1) {
					sqlString.append(" and ");
				}
				sqlString.append(" ( ");
				sqlString.append(priceCheckString);
				sqlString.append(" ) ");
				i2 = 1;
			}
			if (typeCheckString.length() != 0) {
				if (i2 == 1) {
					sqlString.append(" and ");
				}
				sqlString.append(" ( ");
				sqlString.append(typeCheckString);
				sqlString.append(" ) ");
				i3 = 1;
			}
		}
		sqlString.append(orderString);
		// System.out.println(sqlString);

		Query q = s.createQuery(sqlString.toString());
		int bigPage = page / Constant.PAGE_RETURN;
		// System.out.println("page"+page);
		// System.out.println("bigPage"+bigPage);
		q.setFirstResult(bigPage * Constant.PAGE_LENGTH);
		q.setMaxResults(Constant.PAGE_RETURN * Constant.PAGE_LENGTH);
		List<Scheme> returnScheme = q.list();
		return returnScheme;

	}

	@Override
	public long getFilterResultLength(List<Integer> brandList,
			List<Integer> priceList, List<Integer> typeList) {
		StringBuffer brandCheckString = new StringBuffer();
		StringBuffer priceCheckString = new StringBuffer();
		StringBuffer typeCheckString = new StringBuffer();
		Session s = sessionFactory.getCurrentSession();
		if (brandList == null) {
			brandCheckString = new StringBuffer();

		} else {
			Iterator<Integer> iter = brandList.iterator();
			int i = 0;
			while (iter.hasNext()) {

				if (i == 0) {
					i = 1;
				} else {
					brandCheckString.append(" or ");
				}

				Query q = (Query) s
						.createQuery("from Value v where v.key=:key");
				q.setInteger("key", iter.next());
				Value v = (Value) q.uniqueResult();
				String str1 = v.getValue();

				if (str1.equals(BusinessConstant.BARND_CONSTANT[0])) {
					brandCheckString.append(" ( s.cpu.brand.brand='Intel') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[1])) {
					brandCheckString.append(" ( s.cpu.brand.brand='AMD') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[2])) {
					brandCheckString
							.append(" (s.graphics.coreManu.brand='NVIDIA') ");
				}

				else if (str1.equals(BusinessConstant.BARND_CONSTANT[3])) {
					brandCheckString
							.append(" (s.graphics.coreManu.brand='AMD-ATI') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[4])) {
					brandCheckString
							.append(" (s.graphics.coreManu.brand='AMD') ");
				} else if (str1.equals(BusinessConstant.BARND_CONSTANT[5])) {
					brandCheckString.append(" (s.graphics is null) ");
				}
			}
		}
		if (priceList == null) {
			priceCheckString = new StringBuffer();

		} else {
			Iterator<Integer> iter = priceList.iterator();
			int i = 0;
			while (iter.hasNext()) {
				if (i == 0) {
					i = 1;
				} else {
					priceCheckString.append(" or ");
				}
				Query q = (Query) s
						.createQuery("from Value v where v.key=:key");
				q.setInteger("key", iter.next());
				Value v = (Value) q.uniqueResult();
				String str1 = v.getValue();

				String str2 = str1.substring(0, str1.indexOf('元'));

				if (str2.indexOf('-') != -1) {
					priceCheckString.append(" (s.price>"
							+ str2.substring(0, str2.indexOf('-'))
							+ " and s.price<="
							+ str2.substring(str2.indexOf('-') + 1) + ") ");
				}
				if (str1.indexOf('上') != -1) {
					priceCheckString.append(" (s.price>" + str2 + ") ");
				}
				if (str1.indexOf('下') != -1) {
					priceCheckString.append(" (s.price<=" + str2 + ") ");
				}

			}
		}

		if (typeList == null) {
			typeCheckString = new StringBuffer();
		} else {
			Iterator<Integer> iter = typeList.iterator();
			int i = 0;
			while (iter.hasNext()) {
				if (i == 0) {
					i = 1;
				} else {
					typeCheckString.append(" or ");
				}
				Query q = (Query) s
						.createQuery("from Value v where v.key=:key");
				q.setInteger("key", iter.next());
				Value v = (Value) q.uniqueResult();
				String str1 = v.getValue();
				if (str1.equals(BusinessConstant.TYPE_CONSTANT[0])) {
					typeCheckString.append(" (s.type.id=2081) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[1])) {
					typeCheckString.append(" (s.type.id=2082) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[2])) {
					typeCheckString.append(" (s.type.id=2083) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[3])) {
					typeCheckString.append(" (s.type.id=2084) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[4])) {
					typeCheckString.append(" (s.type.id=2085) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[5])) {
					typeCheckString.append(" (s.type.id=2086) ");
				} else if (str1.equals(BusinessConstant.TYPE_CONSTANT[6])) {
					typeCheckString.append(" (s.type.id=2087) ");
				}
			}
		}
		StringBuffer sqlString = new StringBuffer();
		if (brandCheckString.length() == 0 && priceCheckString.length() == 0
				&& typeCheckString.length() == 0)

		{
			sqlString.append("select count(*) from Scheme s ");
		} else {
			sqlString.append("select count(*) from Scheme s where ");
			int i1 = 0;
			int i2 = 0;
			int i3 = 0;
			if (brandCheckString.length() != 0) {
				i1 = 1;
				sqlString.append(" ( ");
				sqlString.append(brandCheckString);
				sqlString.append(" ) ");
			}
			if (priceCheckString.length() != 0) {
				if (i1 == 1) {
					sqlString.append(" and ");
				}
				sqlString.append(" ( ");
				sqlString.append(priceCheckString);
				sqlString.append(" ) ");
				i2 = 1;
			}
			if (typeCheckString.length() != 0) {
				if (i2 == 1) {
					sqlString.append(" and ");
				}
				sqlString.append(" ( ");
				sqlString.append(typeCheckString);
				sqlString.append(" ) ");
				i3 = 1;
			}
		}
		// System.out.println(sqlString);

		Query q = s.createQuery(sqlString.toString());

		long returnScheme = (Long) q.uniqueResult();
		return returnScheme;

	}

	@Override
	public List<Scheme> getFourScheme(int hardwareId, String hardwareType) {
		String sql = null;
		List<Scheme> list = null;
		if (hardwareType.equals(BusinessConstant.H_CHASSIS))
			sql = "select s.* from Scheme s,Chassis c where c.id=s.chassis and c.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_CPU))
			sql = "select s.* from Scheme s,CPU c where c.id=s.cpu and c.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_DISK))
			sql = "select s.* from Scheme s,Disk d where d.id=s.disk and d.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_DRIVER))
			sql = "select s.* from Scheme s,Driver d where d.id=s.driver and d.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_GRAPHICSA))
			sql = "select s.* from Scheme s,Graphics g where g.id=s.graphics and g.id="
					+ hardwareId;

		if (hardwareType.equals(BusinessConstant.H_MAINBOARD))
			sql = "select s.* from Scheme s, Mainboard m where m.id=s.mainboard and m.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_MEMORY))
			sql = "select s.* from Scheme s, Memory m where m.id=s.memory and m.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_MONITOR))
			sql = "select s.* from Scheme s,Monitor m where m.id=s.monitor and m.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_NETWORKADAPER))
			sql = "select s.* from Scheme s,NetworkAdpater n where n.id=s.networkadapter and n.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_POWER))
			sql = "select s.* from Scheme s,Power p where p.id=s.power and p.id="
					+ hardwareId;

		if (hardwareType.equals(BusinessConstant.H_RADIATOR))
			sql = "select s.* from Scheme s,Radiator r where r.id=s.radiator and r.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_SOUNDBOX))
			sql = "select s.* from Scheme s and SoundBox d where d.id=s.soundbox and d.id="
					+ hardwareId;
		if (hardwareType.equals(BusinessConstant.H_SOUNDCARD))
			sql = "select s.* from Scheme s  and SoundCard d where d.id=s.soundcard and d.id= "
					+ hardwareId;
		if (sql == null)
			return null;
		try {
			Session session = sessionFactory.openSession();
			Query query = session.createSQLQuery(sql).addEntity(Scheme.class);
			// System.out.println(sql.toString());
			query.setFirstResult(0);
			query.setMaxResults(Constant.RELATED_SCHEME_NUM);
			// query.setMaxResults(100);
			list = query.list();

		} catch (Exception e) {

		}
		return list;
	}

	@Override
	public boolean deleteSchemeByState(long id) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx1 = null;
		boolean returnInt = false;
		try {
			tx1 = session.beginTransaction();

			Scheme s = (Scheme) session.load(Scheme.class, id);
			s.setUserId((long) 0);
			session.save(s);
			tx1.commit();
		} catch (Exception e) {
			if (null != tx1)
				tx1.rollback();
			e.printStackTrace();
		}

		return returnInt;
	}

	@Override
	public long searchBySchemeNameInteger(String name) {
		Session session = sessionFactory.getCurrentSession();

		long returnLong = 0;
		try {

			String sql = "from count(s) where s.schemeName like '%'||:name||'%' ";
			Query query = session.createQuery(sql);
			query.setParameter("name", name);
			returnLong = (Long) query.uniqueResult();

		} catch (Exception e) {

			e.printStackTrace();
		}
		return returnLong;
	}

	@Override
	public Map<String, List<Scheme>> getPriceLists() {
		List<Value> priceList = valueDaoImpl.getPriceList();
		// System.out.println(priceList.size()+"priceList");

		Iterator<Value> iter = priceList.iterator();
		List<String> querySqlList = new ArrayList<String>();
		Map<String, List<Scheme>> returnScheme = new LinkedMap();
		while (iter.hasNext()) {

			Value v = iter.next();
			String str = v.getValue();
			// System.out.println(str);
			String str1 = str.substring(0, str.indexOf('元'));
			String querySql = new String();
			if (str1.indexOf('-') != -1) {
				querySql = "from Scheme s where s.price>="
						+ str1.substring(0, str1.indexOf('-'))
						+ " and s.price <"
						+ str1.substring(str1.indexOf('-') + 1)
						+ " order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ";

			}
			if (str.indexOf('下') != -1) {
				querySql = "from Scheme s where s.price<="
						+ str1
						+ " order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ";
			}
			if (str.indexOf('上') != -1) {
				querySql = "from Scheme s where s.price>="
						+ str1
						+ " order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ";
			}
			// System.out.println("querySql"+querySql);
			querySqlList.add(querySql);
		}
		Iterator<String> iterString = querySqlList.iterator();
		Session s = sessionFactory.getCurrentSession();
		iter = priceList.iterator();
		while (iterString.hasNext() && iter.hasNext()) {
			Query q = s.createQuery(iterString.next());
			q.setMaxResults(Constant.RELATED_SCHEME_NUM);
			returnScheme.put(iter.next().getValue(), q.list());
		}
		return returnScheme;
	}

	@Override
	public Map<String, List<Scheme>> getTypeLists() {
		List<Value> typeList = valueDaoImpl.getTypeList();

		// System.out.println("typeList"+typeList.size());
		Session s = sessionFactory.getCurrentSession();
		Map<String, List<Scheme>> returnList = new LinkedMap();
		Iterator<Value> sql = typeList.iterator();
		while (sql.hasNext()) {
			Value v = sql.next();
			int id = v.getKey();
			// System.out.println(v.getValue());
			Query q = s.createQuery("from Scheme s where s.type.key=" + id + "order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
			q.setMaxResults(Constant.RELATED_SCHEME_NUM);
			returnList.put(v.getValue(), q.list());
		}
		return returnList;
	}

	@Override
	public Map<String, List<Scheme>> getBrandLists() {
		List<String> str = new ArrayList<String>();
		str.add("from Scheme s  where s.cpu.brand.brand='Intel'  order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
		str.add("from Scheme s where s.cpu.brand.brand='AMD' order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
		str.add("from Scheme s  where s.graphics.coreManu.brand='NVIDIA' order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
		str.add("from Scheme s  where s.graphics.coreManu.brand='AMD-ATI' order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
		str.add("from Scheme s  where s.graphics.coreManu.brand='AMD' order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
		str.add("from Scheme s where s.graphics is null");
		Session s = sessionFactory.getCurrentSession();
		Map<String, List<Scheme>> returnList = new LinkedMap();
		Iterator<String> sql = str.iterator();
		int i = 0;
		while (sql.hasNext()) {

			Query q = s.createQuery(sql.next());
			q.setMaxResults(Constant.RELATED_SCHEME_NUM);
			returnList.put(BusinessConstant.BARND_CONSTANT[i], q.list());
			i++;
		}
		return returnList;
	}

	@Override
	public List<Scheme> getSaleList() {
		List<Scheme> list = new ArrayList<Scheme>();
		Session s = sessionFactory.getCurrentSession();
		Query q = s
				.createQuery("from Scheme s  order by (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
		q.setMaxResults(Constant.RELATED_SCHEME_NUM);
		list = q.list();
		return list;
	}

	@Override
	public List<Scheme> getTimeList() {
		List<Scheme> list = new ArrayList<Scheme>();
		Session s = sessionFactory.getCurrentSession();
		Query q = s
				.createQuery("from Scheme s  order by s.time desc (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale) ");
		q.setMaxResults(Constant.RELATED_SCHEME_NUM);
		list = q.list();
		return list;
	}

	@Override
	public Map<String, List<Scheme>> getRelatedLists(Scheme scheme) {

		Map<String, List<Scheme>> returnScheme = new LinkedMap();
		String brandCpu = "from Scheme s where s.cpu.brand=:brand order by s.time desc (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale)";
		Session s = sessionFactory.getCurrentSession();
		Query q = s.createQuery(brandCpu);

		q.setFirstResult(0);
		q.setMaxResults(Constant.RELATED_SCHEME_NUM);

		q.setParameter("brand", scheme.getCpu().getBrand());
		returnScheme.put(scheme.getCpu().getBrand().getBrand(), q.list());

		String graphicsBrand = "from Scheme s where s.graphics.coreManu=:brand order by s.time desc (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale)";
		q = s.createQuery(graphicsBrand);
		if (scheme.getGraphics() != null) {
			q.setParameter("brand", scheme.getGraphics().getCoreManu());
			q.setFirstResult(0);
			q.setMaxResults(Constant.RELATED_SCHEME_NUM);

			returnScheme.put(scheme.getGraphics().getCoreManu().getBrand(), q.list());
		}
		else
		{
			graphicsBrand="from  Scheme s where s.graphics is null";
			q = s.createQuery(graphicsBrand);
			q.setFirstResult(0);
			q.setMaxResults(Constant.RELATED_SCHEME_NUM);

			returnScheme.put(BusinessConstant.BARND_CONSTANT[5], q.list());
		}
		String priceString = "";

		int price = scheme.getPrice();

		if (price < 3000) {

			priceString = "from Scheme s where s.price<3000 order by s.time desc (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale)";
			q = s.createQuery(priceString);
			q.setFirstResult(0);
			q.setMaxResults(Constant.RELATED_SCHEME_NUM);
			returnScheme.put("3000元以下", q.list());

		}
		if (price > 10000) {
			priceString = "from Scheme s where s.price>10000 order by s.time desc (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale)";
			q = s.createQuery(priceString);
			q.setFirstResult(0);
			q.setMaxResults(Constant.RELATED_SCHEME_NUM);
			returnScheme.put("10000元以上", q.list());

		}
		int minprice = (price / 1000) * 1000;
		int maxprice = minprice + 1000;
		priceString = "from Scheme s where s.price>=:minprice and s.price<:maxprice order by s.time desc (TO_DATE(to_char(sysdate,'YYYY-MM-DD'),'YYYY-MM-DD') - TO_DATE(TO_CHAR(s.time, 'YYYY-MM-DD'),'YYYY-MM-DD'))/(1+s.sale)";
		q = s.createQuery(priceString);
		q.setInteger("minprice", minprice);
		q.setInteger("maxprice", maxprice);
		q.setFirstResult(0);
		q.setMaxResults(Constant.RELATED_SCHEME_NUM);
		returnScheme.put(minprice + "-" + maxprice + "之间", q.list());
		String typeString = "from Scheme s where s.type=:type";
		q = s.createQuery(typeString);

		q.setParameter("type", scheme.getType());
		q.setFirstResult(0);
		q.setMaxResults(Constant.RELATED_SCHEME_NUM);
		returnScheme.put(scheme.getType().getValue(), q.list());

		return returnScheme;
	}

}
